import asyncio
from tortoise import Tortoise
from app.models.user import User, UserRole
from app.core.config import TORTOISE_ORM
from tortoise.contrib.pydantic import pydantic_model_creator

async def init_db():
    await Tortoise.init(
        config=TORTOISE_ORM
    )
    # 添加 safe=True 参数以避免覆盖现有表
    await Tortoise.generate_schemas(safe=True)

async def create_super_admin():
    # 初始化数据库连接
    await init_db()
    

    # 获取用户输入
    print("请输入超级管理员信息：")
    username = input("用户名: ")
    phone = input("手机号：")
    password = input("密码: ")
    email = input("邮箱: ")
    real_name = input("真实姓名: ")

    # 创建超级管理员
    try:
        admin = await User.create_user(
            username=username,
            phone = phone,
            password=password,
            email=email,
            real_name = real_name,
            role=UserRole.SUPER_ADMIN
        )
        print("\n超级管理员创建成功！")
        print(f"用户名: {admin.username}")
        print(f"邮箱: {admin.email}")
    except Exception as e:
        print("\n创建超级管理员时出错:", str(e))
    finally:
        await Tortoise.close_connections()

if __name__ == "__main__":
    asyncio.run(create_super_admin())
